package com.sunnada.edu.business.worker.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.business.attendance.service.AttendanceMethods;
import com.sunnada.edu.business.attendance.service.AttendanceService;
import com.sunnada.edu.business.worker.service.WorkerMethods;
import com.sunnada.edu.business.worker.service.WorkerService;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Utils;

/***
 * explain 工人合同控制层
 * 
 * @author chs 2015-5-12 version 1.0
 */
@Controller
@RequestMapping("/workerController")
public class WorkerController extends BasicController {
	@Resource
	WorkerService workerService;
	@Resource
	AttendanceService attendanceService;
	/** 工人合同首页 */
	@RequestMapping("/worker")
	public String worker(HttpServletRequest request) {
		String curr = request.getParameter("curr");
		request.setAttribute("curr", curr);
		// 当前路径
		String moduleId = request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		return "business/worker/worker";
	}

	/** 工人合同查询 */
	@RequestMapping("/workerSearch")
	public void workerSearch(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		Page page = new Page();
		page.setRowCount(workerService.getWorkerCots(params));
		page.setPageMap(request, page, params);
		List<?> workerList = workerService.findWorkers(params);
		JSONObject json = new JSONObject();
		json.put("list", workerList);
		json.put("params", params);
		printInfo(response, json);
	}

	/** 工人合同工程查询 */
	@RequestMapping("/workerProjectSearch")
	public void workerProjectSearch(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		params.put("SUNNADA_PAGE_SIZE",
				request.getParameter("SUNNADA_PAGE_SIZE"));
		Page page = new Page();
		page.setRowCount(workerService.getWorkerProjectCots(params));
		page.setPageMap(request, page, params);
		List<?> workerList = workerService.findWorkerProjects(params);
		JSONObject json = new JSONObject();
		json.put("list", workerList);
		json.put("params", params);
		printInfo(response, json);
	}

	/** 工人合同工程查询 */
	@RequestMapping("/workerJcqkSearch")
	public void workerJcqkSearch(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		params.put("SUNNADA_PAGE_SIZE",
				request.getParameter("SUNNADA_PAGE_SIZE"));
		Page page = new Page();
		page.setRowCount(workerService.getWorkerJcqkCots(params));
		page.setPageMap(request, page, params);
		List<?> workerList = workerService.findWorkerJcqks(params);
		JSONObject json = new JSONObject();
		json.put("list", workerList);
		json.put("params", params);
		printInfo(response, json);
	}

	/** 工人合同添加页面 */
	@RequestMapping("/workerAdd")
	public String workerAdd(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		return "business/worker/worker_add";
	}

	/** 工人合同修改页面 */
	@RequestMapping("/workerUpdate")
	public String workerUpdate(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		Map worker = workerService.getWorkerByRecId(params);
		request.setAttribute("worker", worker);
		return "business/worker/worker_update";
	}

	/** 工人合同详情页面 */
	@RequestMapping("/workerDetail")
	public String workerDetail(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		Map worker = workerService.getWorkerByRecId(params);
		request.setAttribute("worker", worker);
		return "business/worker/worker_detail";
	}

	/** 工人合同保存功能 */
	@RequestMapping("/workerSave")
	public void workerSave(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		String recId = params.get("recId");
		String photoPath = params.get("photoPath");
		params.put("photo", photoPath);// 照片路径

		if (Utils.isNotNull(recId)) {
			msg = workerService.workerUpdate(request, params);
		} else {
			//公众自定义
			String worktype=params.get("worktype");
			String workerTypeDiy=params.get("workerTypeDiy");
			if(worktype.equals("diy")){
				params.put("worktype", workerTypeDiy);
			}
			msg = workerService.workerAdd(request, params);

		}
		printInfo(response, msg);
	}

	/** 工人合同删除功能 */
	@RequestMapping("/workerDelete")
	public void workerDelete(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		String condition = "";
		String l = null;
		if(Utils.isNotNull(recIds)){
			condition = "AND T.REC_ID IN("+Utils.getSqlSplitStr(recIds)+")";
			params.put("condition", condition);
		    l = attendanceService.getWorkers(params);
		}
		msg = workerService.workerDelete(params);
		if(!msg.equals("删除失败!")){
			AttendanceMethods.AttendanceDel(l);
		}
		printInfo(response, msg);
	}

	/** 工人合同重置密码功能 */
	@RequestMapping("/resetPwd")
	public void resetPwd(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", recIds);
		msg = workerService.workerPwdReset(params);
		printInfo(response, msg);
	}

	/** 工人合同停用 */
	@RequestMapping("/workerDisable")
	public void workerDisable(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = workerService.workerDisable(params);
		printInfo(response, msg);
	}

	/** 工人合同启用 */
	@RequestMapping("/workerEnable")
	public void workerEnable(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = workerService.workerEnable(params);
		printInfo(response, msg);
	}

	/** 工人合同导出 */
	@RequestMapping("/exportWorker")
	public void exportWorker(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		String groupIds = params.get("groupIds");
		if (Utils.isNotNull(groupIds)) {
			// 班组模块导出班组中的工人合同所添加的条件，实现共用一个导出功能
			params.put("groupIds", Utils.getSqlSplitStr(groupIds));
		}
		try {
			workerService.exportWorker(params, request, response);
		} catch (Exception e) {
			printInfo(response,
					"<script>alert('导出失败，请稍后重试！');window.history.go(-1);</script>");
		}
	}

	/** 工人合同信息导入记录界面 */
	@RequestMapping("/workerImportpage")
	public String workerImportpage(HttpServletRequest request) {
		return "business/worker/worker_importpage";
	}

	/** 工人合同照片上传功能 */
	@RequestMapping("/photoUpload")
	public void photoUpload(HttpServletRequest request,
			HttpServletResponse response) {
		String savePath = "";
		// 创建一个通用的多部分解析器
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		// 判断 request 是否有文件上传,即多部分请求
		if (multipartResolver.isMultipart(request)) {
			// 转换成多部分request
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			// 取得request中的所有文件名
			Iterator<String> iter = multiRequest.getFileNames();
			while (iter.hasNext()) {
				// 取得上传文件
				MultipartFile file = multiRequest.getFile(iter.next());
				String fileName = file.getOriginalFilename();// 原文件名
				String fileType = file.getContentType();// 原文件类型
				String name = file.getName();// form表单字段name属性值

				// 存文件
				if ("photo".equals(name)) {
					savePath = workerService.FileSave(file, request);

				}
			}
		}
		printInfo(response, savePath);
	}

	/** 合同附件查询 */
	@RequestMapping("/getWorkerContracts")
	public void getWorkerContracts(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		List<?> projectList = WorkerMethods.getContractList(params);
		JSONObject json = new JSONObject();
		json.put("list", projectList);
		json.put("params", params);
		printInfo(response, json);
	}

	/** 合同附件删除 */
	@RequestMapping("/workerContractDel")
	public void workerContractDel(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		int delCount = WorkerMethods.delContractById(params);
		if (delCount == 0) {
			msg = "合同附件删除失败!";
		} else {
			msg = "";
		}
		printInfo(response, msg);
	}

	/** 退场解约 */
	@RequestMapping("/contractDisable")
	public void contractDisable(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		int delCount = WorkerMethods.contractDisable(params);
		if (delCount == 0) {
			msg = "退场解约失败!";
		} else {
			msg = "";
		}
		printInfo(response, msg);
	}

	/**
	 * 弹出框展示工人合同（选择器）带分页
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/selectWorker")
	public void selectWorker(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		Page page = new Page();
		String wkIds = request.getParameter("wokIds");
		String wkId = request.getParameter("wkId");
		String condition = "";
		if (Utils.isNotNull(wkIds)) {
			condition += "AND T.JOBNO NOT IN(" + Utils.getSqlSplitStr(wkIds)
					+ ")";
		}
		if (Utils.isNotNull(wkId)) {
			condition += "AND T.JOBNO NOT IN(" + Utils.getSqlSplitStr(wkId)
					+ ")";
		}
		params.put("condition", condition);
		params.put("SUNNADA_PAGE_SIZE",
				request.getParameter("SUNNADA_PAGE_SIZE"));
		page.setRowCount(workerService.selectWorkerCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = workerService.selectWorkers(params);
		JSONObject json = new JSONObject();
		json.put("list", userList);
		json.put("params", params);
		printInfo(response, json);
	}

	/**
	 * 无分页展示工人合同列表
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getWorkerList")
	public void getWorkerList(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		String workerIds = request.getParameter("workerIds");
		String condition = "";
		if (Utils.isNotNull(workerIds)) {
			condition += " AND T.REC_ID IN(" + Utils.getSqlSplitStr(workerIds)
					+ ")";
		}

		params.put("condition", condition);
		List<?> userList = workerService.getWorkerList(params);
		JSONObject json = new JSONObject();
		json.put("list", userList);
		json.put("params", params);
		printInfo(response, json);
	}

	
}
